﻿using System;
using System.Linq;
using CRM.Models.Email;

namespace CRM.ViewModels.Email
{
    public class EmailSendHistoryVM
    {
        private readonly MyDbContext _dbContext;
        public EmailSendHistoryVM(MyDbContext dbContext)
        {   
            _dbContext = dbContext;
        }
        public int Id { get; set; }
        public int UserId { get; set; }
        public int OemId { get; set; }
        public int CompanyId { get; set; }
        public int GroupId { get; set; }
        public string Name { get; set; }
        public string ReceiverEmail { get; set; }
        public EmailSendHistory.SendingTypeEnum SendingType { get; set; }
        public string SenderEmail { get; set; }
        public bool State { get; set; }
        public int Source { get; set; }
        public string MarketingTaskName { get; set; }
        public string Action { get; set; }
        public EmailSendHistory NewModel(MyDbContext dbContext,Models.User user)
        {
            var marketingTaskNameValue =
                dbContext.EmailMarketing
                    .Where(m => m.OemId == user.OemId && m.CompanyId == user.CompanyId && m.GroupId == user.GroupId && m.UserId == user.Id)
                    .Where(m => m.Id == Source).Select(m => m.Title).First();
            return new() {
                UserId = user.Id,
                OemId = user.OemId,
                CompanyId = user.CompanyId,
                GroupId = user.GroupId,
                Name = Name,
                ReceiverEmail = ReceiverEmail,
                SendingType = SendingType,
                SenderEmail = SenderEmail,
                State = State,
                Source = Source,
                MarketingTaskName = marketingTaskNameValue,
                CreatedAt = DateTime.Now,
                UpdatedAt = DateTime.Now,
            };
        }
        
        public record emailResultRecord(string MessageUUID,long MessageID,string MessageHref,string Email);
        public static EmailSendHistory NewMailjetModel(EmailMarketing email,string receiverEmail,string senderEmail,AddresseeObj customerData,emailResultRecord emailResult)
        {
            EmailSendHistory result =  new(){
                UserId = email.UserId,
                OemId = email.OemId,
                CompanyId = email.CompanyId,
                GroupId = email.GroupId,
                Name = customerData.Name,
                ReceiverEmail = receiverEmail,
                SendingType = EmailSendHistory.SendingTypeEnum.Server,
                EmailPlatform = EmailPlatform.Mailjet,
                SenderEmail = senderEmail,
                State = true,
                Source = email.Id,
                MarketingTaskName = email.Title,
                MessageUUID = emailResult.MessageUUID,
                MessageID = emailResult.MessageID,
                MessageHref = emailResult.MessageHref
            };
            return result;
        }
        
        public static EmailSendHistory NewRushmailModel(EmailMarketing email, AddresseeObj customerData)
        {
            EmailSendHistory result =  new(){
                UserId = email.UserId,
                OemId = email.OemId,
                CompanyId = email.CompanyId,
                GroupId = email.GroupId,
                Name = customerData.Name,
                ReceiverEmail = customerData.Addressee,
                SendingType = EmailSendHistory.SendingTypeEnum.Server,
                EmailPlatform = EmailPlatform.Rushmail,
                SenderEmail = "",
                State = true,
                Source = email.Id,
                MarketingTaskName = email.Title
            };
            return result;
        }
        
        
        
        //public SocialMarketingTask EditModel(SocialMarketingTask task)
        //{
        //    task.Name = Name;
        //    task.Content = Content;
        //    task.ImagesStr = ImagesStr;
        //    task.UpdatedAt = DateTime.Now;
        //    return task;
        //}
        
    }
    public class EmailSendHistoryListQuery
    {
        public DateTime[] CreatedAt { get; set; }

        public string pageName { get; set; }
        public int[] Details { get;set; }

        public int Flag => Details?.Where(m => m > 0).Aggregate(0, (i, i1) => i | i1)??0;
        public int NegativeFlag => Details?.Where(m => m < 0).Aggregate(0, (i, i1) => i | -i1)??0;
        public bool? State { get; set; }
        public EmailSendHistory.SendingTypeEnum? SendingType =>
            pageName switch
            {
                "EmailServerSend" or "EmailRushEmailSend" => EmailSendHistory.SendingTypeEnum.Server,
                "EmailSendFree" => EmailSendHistory.SendingTypeEnum.Free,
                _ => null
            };
        public EmailPlatform? EmailPlatform =>
            pageName switch
            {
                "EmailServerSend" => Models.Email.EmailPlatform.Mailjet,
                "EmailRushEmailSend" => Models.Email.EmailPlatform.Rushmail,
                _ => null
            };

        public  string MarketingTaskName { get; set; }
        public  int Limit { get; set; }
        public  int Page { get; set; }
    }
}